﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ControlYSeguimiento.Domain;
using ControlYSeguimiento.Logging;

namespace ControlYSeguimiento_Web_App.Admin
{
    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Panel panelLogBox = (Panel)Master.FindControl("PanelLogBox");
            panelLogBox.Visible = false;
            Panel panelNewUserBox = (Panel)Master.FindControl("PanelNewUserBox");
            panelNewUserBox.Visible = false;

            if (Session["userLogged"] == null)
            {
                PanelContent.Visible = false;
                PanelLogUser.Visible = true;
            }
            else
            {
                PanelContent.Visible = true;
                PanelLogUser.Visible = false;
            }
        }

        protected void btnLogUser_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                User thisUser = Facade.Instance.GetUserByUsernameAndPassword(txtUser.Text, txtPassword.Text);
                if (thisUser != null)
                {
                    
                    Session["userLogged"] = thisUser;
                    lblError.Text = String.Empty;
                    lblError.Visible = false;
                    PanelContent.Visible = true;
                    PanelLogUser.Visible = false;
                }
                else
                {
                    lblError.Text = "User not found";
                    lblError.Visible = true;
                    PanelContent.Visible = false;
                    PanelLogUser.Visible = true;
                }
                
            }
        }

        protected void ObjectDataSourceTransactions_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            Logger.Instance.Debug(this, "Inicializando ObjectDataSourceTransactions_ObjectCreating...");
            e.ObjectInstance = Facade.Instance;
        }
    }
}